<?php

function SynonymListFolderData() {
  global $_GET, $path, $style, $session_id, $data, $term_Thing_ID, $language, $header;
  include "controls.php";

  $filename = $path[$style]["tpl"]."/attrdata.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
  preg_match("/\%REPDAT\%(.*?)\%REPDAT\%/s", $matches[1], $datmatches);

  $grp0 = new ControlGroup(0, getMessage("Synonyms"), $matches[1], $datmatches, "");

  //$udata = str_replace($datmatches[0], "", $matches[1]);
  //$udata = str_replace("%GROUP_ID%", "0", $udata);
  //$udata = str_replace("%GROUP_TL%", , $udata);

  header ($header["xml"]);
  return str_replace($matches[0], $grp0->getResult(), $contents);
  //return str_replace($matches[0], $udata, $contents);
}

function getSynonyms($class_id, $matches, $session_id, $element){
  global $_GET, $undef_id, $language;
  $synonym_label = substr($_GET["uid"], 0, 3);
  $next_class_id = $class_id;
  $class1_id = $next_class_id;
  $sql_str = "SELECT t.text FROM class c, term t WHERE t.id=c.term_id and c.id=".$class_id;
  getData($sql_str, $next_data);
  $rep_cont = "";
  switch ($element) {
    case "class":
      $sql_str = "SELECT s.id, s.text FROM synonyms s, class c WHERE s.term_id = c.term_id and s.lang_id = ".$language["content"]." and c.id = ".$next_class_id;
      break;
    case "attribute":
      $sql_str = "SELECT s.id, s.text FROM synonyms s, attr a WHERE s.term_id = a.term_id and s.lang_id = ".$language["content"]." and a.id = ".$next_class_id;
      break;
  };
  $i_max = getMultData($sql_str, $ontosdata);

  for ($i = 0; $i < $i_max; $i++) {
    $tmp_attr = str_replace("%CLASS_ID%", $synonym_label.$ontosdata[$i]["id"], $matches[1]);
    if (isset($_GET["sel"]) && $_GET["sel"] == $synonym_label.$ontosdata[$i]["id"])
      $tmp_attr = str_replace("%ACT%", "select", $tmp_attr);
    $tmp_attr = str_replace("%CLASS_NM%", $ontosdata[$i]["text"], $tmp_attr);
    if ($inherited)
      $tmp_attr = str_replace("%IMG_TYPE%", "attr", $tmp_attr);
    else
      $tmp_attr = str_replace("%IMG_TYPE%", "attr", $tmp_attr);
    $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=synonymdata&amp;sid=".
    $session_id."&amp;uid=".$synonym_label.$ontosdata[$i]["id"], $tmp_attr);

    $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);

    if (isset($_GET["sel"]) &&
      ($_GET["sel"] == $synonym_label.$ontosdata[$i]["id"] || ($_GET["sel"] == 0 && $i == 0))) {
      $tmp_attr = str_replace("%ACT%", "select", $tmp_attr);
    }
    $rep_cont .= $tmp_attr;
  };
  return $rep_cont;
};


// get list of functional relations and return XML contains it
function SynonymList(){
  global $_GET, $data, $path, $style, $session_id, $language, $header;
  $filename = $path[$style]["tpl"]."/classes.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
  $rep_cont = getSynonyms(substr($_GET["uid"], 7), $matches, $session_id, "class");
  $contents = str_replace($matches[0], $rep_cont, $contents);
  header ($header["xml"]);
  return $contents;
};

// get functional relations data
function SynonymData($new_id) {
  global $_GET, $path, $style, $session_id, $data, $term_Thing_ID, $language, $header;
  include "controls.php";

  $filename = $path[$style]["tpl"]."/attrdata.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
  preg_match("/\%REPDAT\%(.*?)\%REPDAT\%/s", $matches[1], $datmatches);
  preg_match("/\%REPOPT\%(.*?)\%REPOPT\%/s", $datmatches[1], $optmatches);
  $rep_cont = "";

  $SynID = substr($_GET["uid"], 3);

  $dlg_form = "";

  // select synonyms name
  getData("SELECT text FROM synonyms WHERE id=".$SynID, $synonym_name);

  $udata = "";
  if ($data["type"] >= 1) { //user
    //New name
    if ($new_id > -1){
      // !!!!!!!!!!!!!!!!!!!! after update
      $contents = str_replace("%NAME%", $synonym_name["text"], $contents);
      $contents = str_replace("%ID%", $_GET["uid"], $contents);
      $contents = str_replace("%DatHref%", "index.php?trg=ontoedit&amp;act=synonymdata&amp;sid=".$session_id."&amp;uid=".$_GET["uid"], $contents);
    };

    // Synonyms information, like name
    $grp0 = new ControlGroup(0, getMessage("Synonyms"), $matches[1], $datmatches, "");
    $grp0->addControl("Text", "osname", getMessage("Synonyms name"), $synonym_name["text"]);
    $grp0->addControl("Hidden", "ohidd", "", $SynID);
    $grp0->addControl("Button", "obutn", getMessage("Save"), "'0','osname','ohidd'");

    $dlg_form .= $grp0->getResult();

    //Action: return to the ontologies
    $grp1 = new ControlGroup(1, getMessage("Action"), $matches[1], $datmatches, "");
    $grp1->addControl("Button", "", getMessage("Ontologies"), "'wd_tree', 'index.php?trg=ontoedit&amp;sid=".$session_id."&amp;act=ontolist&amp;sel=".$onto_name["id"]."'");

    $dlg_form .= $grp1->getResult();
  }
  header ($header["xml"]);
  //return str_replace($matches[0], $udata.$btns, $contents);
  return str_replace($matches[0], $dlg_form, $contents);
}

// Update inf. about synonyms
function updatesynonym() {
  global $_GET, $data, $path, $style, $session_id, $term_Thing_ID, $language, $header;
  $comment = "";
  $_GET["osname"] = utf8Encode($_GET["osname"]);
  $_GET["ohidd"] = utf8Encode($_GET["ohidd"]);

  $filename = $path[$style]["tpl"]."/action.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);
  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);

  if ($data["type"] >= 1) { //user
  // if user have a rights for adding
    if ($_GET["osname"] <> ""){
      //update synonyms name
      $sql_str = "UPDATE synonyms SET text = '".$_GET["osname"]."' WHERE id=".$_GET["ohidd"];
      insertRecord($sql_str);
    };
  };
  $contents = SynonymData(0);
  header ($header["xml"]);
  return $contents;
}

// create new synonym
function createSynonym() {
  global $_GET, $data, $path, $style, $session_id, $language, $header;

  $filename = $path[$style]["tpl"]."/action.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);
  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);

  if ($data["type"] >= 1) { //user
    // if user have a rights for adding
      insertRecord("INSERT INTO synonyms (term_id, text, lang_id) SELECT term_id, 'Name', '".$language["content"]."' FROM class WHERE id=".substr($_GET["uid"], 7));
      $prev_id = GetPreviousInsertedID();
      if ($prev_id > 0){
        $contents = str_replace("%SCCSS%", "1", $contents);
        $childs[0]["id"] = -1;
      } else {
        $childs[0]["id"] = "0";
        $contents = str_replace("%SCCSS%", "0", $contents);
        $datatmp["id"] = 0;
      };
  } else {
    $childs[0]["id"] = "0";
    $contents = str_replace("%SCCSS%", "0", $contents);
    $datatmp["id"] = 0;
  }
  $tmp_cont = str_replace("%NODE_ID%", "syn".$prev_id, $matches[1]);
  $tmp_cont = str_replace("%NODE_NM%", getMessage("Name"), $tmp_cont);
  $tmp_cont = str_replace("%IMG_TYPE%", "attr", $tmp_cont);
  $tmp_cont = str_replace("%NODE_LN%", "index.php?trg=ontoedit&amp;act=synonymdata&amp;sid=".$session_id."&amp;uid=".$_GET["uid"], $tmp_cont);
  $contents = str_replace($matches[0], $tmp_cont, $contents);
  $contents = str_replace("%XMLSRC%", "index.php?trg=ontoedit&amp;act=synonymlist&amp;sid=".$session_id."&amp;uid=".$_GET["uid"]."&amp;sel=".$_GET["sel"], $contents);
  $contents = str_replace("%ACT_ID%", "Add", $contents);
  $contents = str_replace("%PARENT%", $_GET["uid"], $contents);

  header ($header["xml"]);
  return $contents;
}

function deleteSynonym() {
  global $_GET, $data, $path, $style, $session_id, $header;
  $filename = $path[$style]["tpl"]."/action.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);

  if ($data["type"] >= 1) { //user
    $sql = "SELECT c.id FROM class c, synonyms s WHERE c.term_id = s.term_id and s.id=".substr($_GET["uid"], 3);
    if (getdata($sql, $class_id) > 0){
      //Delete functional constraint
      insertRecord($sql = "DELETE FROM synonyms WHERE id=".substr($_GET["uid"], 3));
      $contents = str_replace("%SCCSS%", "1", $contents);
      $contents = str_replace($matches[0], "", $contents);
      $contents = str_replace("%ACT_ID%", "Delete", $contents);
      $contents = str_replace("%PARENT%", $_GET["uid"], $contents);
      $contents = str_replace("%SELECT%", "synlist".$class_id["id"], $contents);
    }else{
      $contents = str_replace("%SCCSS%", "0", $contents);
    };
  }else{
    $contents = str_replace("%SCCSS%", "0", $contents);
  };
  header ($header["xml"]);
  return $contents;
}

?>